<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>黑白照片转换工具 - 在线工具集合</title>
    <script src="../utils/toast.js"></script>
    <link rel="stylesheet" href="../utils/styles.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
            color: #fff;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .header p {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1.1em;
        }

        .upload-area {
            border: 2px dashed rgba(255, 255, 255, 0.2);
            border-radius: 20px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 30px;
            background: rgba(255, 255, 255, 0.05);
        }

        .upload-area:hover {
            border-color: #00ff88;
            background: rgba(255, 255, 255, 0.1);
        }

        .preview-container {
            display: flex;
            gap: 20px;
            margin: 30px 0;
            flex-wrap: wrap;
            justify-content: center;
        }

        .preview-box {
            flex: 1;
            min-width: 300px;
            max-width: 500px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }

        .preview-box img {
            width: 100%;
            height: auto;
            display: block;
        }

        .preview-label {
            position: absolute;
            top: 10px;
            left: 10px;
            background: rgba(0, 0, 0, 0.6);
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 0.9em;
        }

        .controls {
            background: rgba(255, 255, 255, 0.05);
            border-radius: 12px;
            padding: 20px;
            margin: 20px 0;
        }

        .control-group {
            margin-bottom: 15px;
        }

        .control-group label {
            display: block;
            margin-bottom: 8px;
            color: rgba(255, 255, 255, 0.9);
        }

        .control-group input[type="range"] {
            width: 100%;
            margin: 10px 0;
        }

        .action-buttons {
            display: flex;
            gap: 20px;
            justify-content: center;
            margin-top: 30px;
        }

        .action-btn {
            padding: 15px 30px;
            border: none;
            border-radius: 12px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 255, 136, 0.3);
        }

        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #00ff88;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .back-link:hover {
            transform: translateX(-5px);
        }

        @media (max-width: 768px) {
            .preview-container {
                flex-direction: column;
            }

            .preview-box {
                min-width: 100%;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="../index.html" class="back-link">← 返回首页</a>
        
        <div class="header">
            <h1>黑白照片转换工具</h1>
            <p>将彩色照片转换为黑白效果，支持调节对比度和亮度</p>
        </div>

        <div class="upload-area" id="uploadArea">
            <div class="upload-text">点击或拖拽图片到这里</div>
            <input type="file" id="imageInput" accept="image/*" style="display: none;">
        </div>

        <div class="preview-container">
            <div class="preview-box">
                <div class="preview-label">原图</div>
                <img id="originalPreview">
            </div>
            <div class="preview-box">
                <div class="preview-label">黑白效果</div>
                <img id="bwPreview">
            </div>
        </div>

        <div class="controls">
            <div class="control-group">
                <label>对比度 <span id="contrastValue">100%</span></label>
                <input type="range" id="contrastInput" min="0" max="200" value="100">
            </div>
            <div class="control-group">
                <label>亮度 <span id="brightnessValue">100%</span></label>
                <input type="range" id="brightnessInput" min="0" max="200" value="100">
            </div>
        </div>

        <div class="action-buttons">
            <button class="action-btn" id="convertBtn">转换</button>
            <button class="action-btn" id="downloadBtn">下载</button>
        </div>
    </div>

    <script>
        const uploadArea = document.getElementById('uploadArea');
        const imageInput = document.getElementById('imageInput');
        const originalPreview = document.getElementById('originalPreview');
        const bwPreview = document.getElementById('bwPreview');
        const contrastInput = document.getElementById('contrastInput');
        const brightnessInput = document.getElementById('brightnessInput');
        const contrastValue = document.getElementById('contrastValue');
        const brightnessValue = document.getElementById('brightnessValue');
        let originalImage = null;

        // 文件处理
        uploadArea.addEventListener('click', () => imageInput.click());
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.style.borderColor = '#00ff88';
        });
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.style.borderColor = 'rgba(255, 255, 255, 0.2)';
        });
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            const file = e.dataTransfer.files[0];
            if (file && file.type.startsWith('image/')) {
                handleImage(file);
            }
        });

        imageInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (file) {
                handleImage(file);
            }
        });

        function handleImage(file) {
            const reader = new FileReader();
            reader.onload = (e) => {
                originalImage = new Image();
                originalImage.onload = () => {
                    originalPreview.src = originalImage.src;
                    convertToBlackAndWhite();
                };
                originalImage.src = e.target.result;
            };
            reader.readAsDataURL(file);
        }

        function convertToBlackAndWhite() {
            if (!originalImage) {
                toast.error('请先上传图片');
                return;
            }

            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            
            canvas.width = originalImage.width;
            canvas.height = originalImage.height;
            
            // 绘制原始图片
            ctx.drawImage(originalImage, 0, 0);
            
            // 获取图片数据
            const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
            const data = imageData.data;
            
            const contrast = contrastInput.value / 100;
            const brightness = brightnessInput.value / 100;
            
            // 转换为黑白并应用对比度和亮度
            for (let i = 0; i < data.length; i += 4) {
                // 转换为灰度
                const gray = (data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114);
                
                // 应用对比度
                const contrastedGray = ((gray / 255 - 0.5) * contrast + 0.5) * 255;
                
                // 应用亮度
                const finalGray = contrastedGray * brightness;
                
                // 确保值在0-255范围内
                const pixelValue = Math.max(0, Math.min(255, finalGray));
                
                data[i] = pixelValue;     // R
                data[i + 1] = pixelValue; // G
                data[i + 2] = pixelValue; // B
            }
            
            ctx.putImageData(imageData, 0, 0);
            bwPreview.src = canvas.toDataURL('image/jpeg', 0.9);
        }

        // 控制器事件监听
        contrastInput.addEventListener('input', () => {
            contrastValue.textContent = `${contrastInput.value}%`;
            convertToBlackAndWhite();
        });

        brightnessInput.addEventListener('input', () => {
            brightnessValue.textContent = `${brightnessInput.value}%`;
            convertToBlackAndWhite();
        });

        // 转换按钮
        document.getElementById('convertBtn').addEventListener('click', convertToBlackAndWhite);

        // 下载按钮
        document.getElementById('downloadBtn').addEventListener('click', () => {
            if (!bwPreview.src) {
                toast.error('请先转换图片');
                return;
            }

            const link = document.createElement('a');
            link.download = 'black_and_white.jpg';
            link.href = bwPreview.src;
            link.click();
            toast.success('开始下载');
        });
    </script>
</body>
</html> 